package com.itbbj.principle.d3_OCP.demo1;

import java.util.Random;
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: jack_yang
 * @Date: 2024/5/6
 * @Description: addition and subtraction
 */
public class Equation {
    protected int leftNum;
    protected int rightNum;
    protected String op;
    protected int result;
    @Override
    public String toString() {
        return leftNum + " " + op + " " + rightNum + " = " + result;
    }

    //1.产生指定区间的随机数
    public int generateRandom(int min, int max) {
        Random random = new Random();
        return random.nextInt(max - min + 1) + min;
    }
    public Equation generateEquation() {
        leftNum = generateRandom(0, 100);
        rightNum = generateRandom(0, 100);
        if(op.equals("+")){
            result = leftNum + rightNum;
        } else if (op.equals("-")) {
            result = leftNum - rightNum;
        }
        this.op = op;
        return this;
    }
}
